$(function () {
    let $lib;
    //页面渲染
    function getList(page,callback) {
        $.ajax({
            url: BigNew.comment_list,
            data:{
                page
            },
            success(backData) {
                console.log(backData);
                let html = template('commentList', backData.data)
                $('tbody').html(html);
                $lib = null;
                //页签组件功能
                $lib = $('#pagination-demo').twbsPagination({
                    totalPages: backData.data.totalPage,
                    visiblePages: 3,
                    prev: '上一页',
                        next: '下一页',
                        first: '首页',
                        last: '尾页',
                    onPageClick: function (event, page) {
                        getList(page);
                    }
                });
                if (typeof callback == 'function') {
                    callback(backData.data.totalPage);
                }
               
            }
        })

    }
    getList(1);
    //页签组件总页数回调函数
function getconutpages(page) {
    if (page != $lib.twbsPagination('getTotalPages')) {
        let currentPage = $lib.twbsPagination('getCurrentPage');

        if (currentPage > page) {
            currentPage = page;
        } else {
            currentPage = 1;
        }
    $lib.twbsPagination('changeTotalPages', page, currentPage);
    console.log('页码匹配')
}
};
  //批准
    $('tbody').on('click', '.btn-pass', function (e) {
        let id = $(this).parent().attr('data-id');
        $.ajax({
            type: 'post',
            url: BigNew.comment_pass,
            data: {
                id
            },
            success(backData) {
                getList($lib.twbsPagination('getCurrentPage'));
            }
        })
    });
    //拒绝
    $('tbody').on('click', '.btn-reject', function (e) {
        let id = $(this).parent().attr('data-id');
        $.ajax({
            type: 'post',
            url: BigNew.comment_reject,
            data: {
                id
            },
            success(backData) {
                getList($lib.twbsPagination('getCurrentPage'));
            }
        })
    });
   // 删除
    $('tbody').on('click', '.btn-delete', function (e) {
        let id = $(this).parent().attr('data-id');
        $.ajax({
            type: 'post',
            url: BigNew.comment_delete,
            data: {
                id
            },
            success(backData) {
                getList($lib.twbsPagination('getCurrentPage'),getconutpages);
            }
        })
    });

})